package org.zhao.nettydemos.decoder;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageCodec;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @Auther: xiaohui.zhao
 * @Date: 2021/9/16 17:02
 * @Description:
 */
@Slf4j
public class Byte2IntegerCodec extends ByteToMessageCodec<Integer> {

    @Override
    public void encode(ChannelHandlerContext ctx, Integer msg, ByteBuf out)throws Exception{
        out.writeInt(msg);
        log.info("write integer = "+msg);
    }

    @Override
    public void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out){
        if(in.readableBytes()>=4){
            int i = in.readInt();
            log.info("decoder i = "+i);
            out.add(i);
        }
    }

}